package model

import (
	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	UserName  string `gorm:"type:varchar(30);commit:用户名"`
	Password  string `gorm:"type:varchar(50);commit:密码"`
	Photo     string `gorm:"type:varchar(225);commit:头像"`
	Nickname  string `gorm:"type:varchar(30);commit:昵称"`
	Signature string `gorm:"type:varchar(30);commit:个性签名"`
}

type Friend struct {
	gorm.Model
	UserId   int64  `gorm:"type:int;commit:用户Id"`
	FriendId int64  `gorm:"type:int;commit:好友ID"`
	Reason   string `gorm:"type:varchar(225);commit:理由"`
	Status   int64  `gorm:"type:int;commit:1:同意,2:待处理,3已拒绝"`
}
type Blacklist struct {
	gorm.Model
	UserId   int64 `gorm:"type:int;commit:用户Id"`
	FriendId int64 `gorm:"type:int;commit:好友ID"`
	Status   int64 `gorm:"type:int;commit:1:同意,2:待处理,3已拒绝"`
}
type Circle struct {
	gorm.Model
	UserId int64  `gorm:"type:int;commit:用户Id"`
	Text   string `gorm:"type:varchar(225);commit:内容"`
}
type Liao struct {
	gorm.Model
	UserId   int64  `gorm:"type:int;commit:用户Id"`
	FriendId int64  `gorm:"type:int;commit:好友ID"`
	Text     string `gorm:"type:varchar(225);commit:内容"`
}

func (l *Liao) LiaoAdd(DB *gorm.DB) error {
	var err error
	err = DB.Create(&l).Error
	return err
}
func (f *Friend) IsFriend(DB *gorm.DB, userid int, FriendId int) (err error) {
	err = DB.Where("user_id = ? and friend_id = ?", userid, FriendId).First(&f).Error
	return err
}

func (b *Blacklist) IsBlacklist(DB *gorm.DB, userid int, FriendId int) (err error) {
	err = DB.Where("user_id = ? and friend_id = ?", userid, FriendId).First(&b).Error
	return err
}

func (c *Circle) CircleList(DB *gorm.DB) (list *[]Circle, err error) {
	err = DB.Model(&c).Order("created_at desc").Find(&list).Error
	return list, err
}

func (c *Circle) CircleAdd(DB *gorm.DB) error {
	var err error
	err = DB.Create(&c).Error
	return err
}

func (b *Blacklist) BlackAdd(DB *gorm.DB) error {
	var err error
	err = DB.Create(&b).Error
	return err
}

func (f *Friend) FriendUpdate(DB *gorm.DB, userId string, status string) error {
	var err error
	err = DB.Model(&f).Where("id = ?", userId).Update("status", status).Error
	return err
}

func (f *Friend) FriendAdd(DB *gorm.DB) error {
	var err error
	err = DB.Create(&f).Error
	return err
}

func (u *User) Register(DB *gorm.DB) error {
	var err error
	err = DB.Create(&u).Error
	return err
}

func (u *User) UserSList(DB *gorm.DB, UserName string, Nickname string) (list *[]User, err error) {

	query := DB.Model(&u)
	if UserName != "" {
		query = query.Where("user_name like ?", "%"+UserName+"%")
	}
	if UserName != "" {
		query = query.Where("nickname like ?", "%"+Nickname+"%")
	}
	err = query.Find(&list).Error
	return list, err
}

func (f *Friend) FriendList(DB *gorm.DB) (list *[]Friend, err error) {
	err = DB.Model(&f).Find(&list).Error
	return list, err
}

func (b *Blacklist) BlacklistList(DB *gorm.DB) (list *[]Blacklist, err error) {
	err = DB.Model(&b).Find(&list).Error
	return list, err
}
